package com.example.listossimage.controller;

import com.example.listossimage.entity.esPo.Book;
import com.example.listossimage.service.BookService;
import org.elasticsearch.action.admin.indices.create.CreateIndexRequest;
import org.elasticsearch.action.admin.indices.create.CreateIndexResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.elasticsearch.core.SearchHits;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.io.Externalizable;
import java.io.IOException;
import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

@RestController
public class BookController {

//    BookService bookService;
//
//    @PostMapping("/book")
//    public Map<String,String> addBook(@RequestBody Book book){
//        bookService.addBook(book);
//        Map<String,String> map = new HashMap<>();
//        map.put("msg","ok");
//        return map;
//    }
//
//    @GetMapping("/book/search")
//    public SearchHits<Book> search(String key){
//        return bookService.searchBook1(key);
//    }

    @Qualifier("restHighLevelClient")
    @Autowired
    RestHighLevelClient client;

    @GetMapping("/create")
    public void createIndex() throws IOException {
        //1、构建 创建索引的请求
        CreateIndexRequest request = new CreateIndexRequest("xk_index");//索引名
        //2、客户端执行请求,获取响应
        CreateIndexResponse response = client.indices().create(request, RequestOptions.DEFAULT);
        //3、打印
        System.out.println("创建成功，创建的索引名为：" + response.index());
    }

}
